<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class OutgoingRequestTransformer extends OutgoingTransformer {
	
	public $xmlData;
	
	public function __construct($bid_request_id, $remote_url, $table_direction = 'Outgoing') {
		
		$member_direction = 'outgoing';
		
		if ($table_direction == 'Incoming'):
			$bid_request_factory = IncomingBidRequestFactory::get_instance();
			$bid_request_host_factory = IncomingBidRequestHostFactory::get_instance();
			$member_direction = 'incoming';
		else:
			$bid_request_factory = OutgoingBidRequestFactory::get_instance();
			$bid_request_host_factory = OutgoingBidRequestHostFactory::get_instance();
		endif;
    	$params[$table_direction . "BidRequestID"] = $bid_request_id; 
    	$bid_request = $bid_request_factory->get_row($params);
    	$bid_request->load_children();
    	
		$params = array();
		$column_name 						= $table_direction . 'BidRequestID';
		$params[$column_name] 				= $bid_request->$column_name;
		$bid_request_host 					= $bid_request_host_factory->get_row($params);
            
		if ($bid_request_host == null):

	   		$class_name 							= $table_direction .  'BidRequestHost';
			$bid_request_host 						= new $class_name();
			$column_name 							= $table_direction . 'BidRequestID';
			$bid_request_host->$column_name			= $bid_request->$column_name;
		endif;
		
		if ($table_direction == 'Incoming'):
			$bid_request_host->RemoteHostAddr	= $remote_url; 
		else:
			$bid_request_host->RemoteURL	= $remote_url; 
		endif;
			
		$bid_request_host->HttpResponseCode		= 400;
		$bid_request_host->save();
		
		$dom_document = new DomDocument("1.0", "ISO-8859-1");

		$dom_element_dadf_request 	= $dom_document->createElement('dadf-request');
		
		$member_name = 'class_member_only_' . $member_direction . '_request_authentication';
		
		if ($bid_request->$member_name != null):
		
			$dom_element_authentication 	= $dom_document->createElement('authentication');
			$member_name			 		= 'class_member_only_' . $member_direction . '_request_authentication';
			$dom_element_username 			= $dom_document->createElement('username', $bid_request->$member_name->Username);
			$dom_element_authentication->appendChild($dom_element_username);
			// change to plain text pass for outgoing
			if ($table_direction == 'Incoming'):
				$dom_element_password 		= $dom_document->createElement('password', $bid_request->$member_name->PasswordMD5);
			else:
				$dom_element_password 		= $dom_document->createElement('password', EncryptUtils::decrypt_pass($bid_request->$member_name->PasswordEncoded));
			endif;

			$dom_element_authentication->appendChild($dom_element_password);
			$dom_element_dadf_request->appendChild($dom_element_authentication);
			
		endif;
		
		$dom_element_adbundles 				= $dom_document->createElement('adbundles');
		
		//echo(count($bid_request->class_member_only_outgoing_request_adbundle_list));
		
		$member_name			 			= 'class_member_only_' . $member_direction . '_request_adbundle_list';
		
		foreach ($bid_request->$member_name as $request_adbundle):
		
			$requestdate 					= $request_adbundle->RequestDate;
			$dom_element_adbundle 			= $dom_document->createElement('adbundle');
			$requestdate 					= TimeUtils::mysql_date_to_xml_date($requestdate);
			$dom_element_requestdate 		= $dom_document->createElement('requestdate', $requestdate);
			$dom_element_adbundle->appendChild($dom_element_requestdate);
			
			$member_name			 		= 'class_member_only_' . $member_direction . '_request_advertisement_list';
			
			foreach ($request_adbundle->$member_name as $request_advertisement):

				$dom_element_advertisement 			= $dom_document->createElement('advertisement');

				$dom_element_code 					= $dom_document->createElement('code');
				
				$member_name			 			= 'class_member_only_' . $member_direction . '_request_code_size_list';
				
				foreach ($request_advertisement->$member_name as $request_code_size):
					
					$dom_element_code_size 			= $dom_document->createElement('size');
					$dom_attribute	 				= $dom_document->createAttribute('name');
					$dom_attribute->value 			= $request_code_size->Name;
					$dom_element_code_size->appendChild($dom_attribute);
					$dom_attribute	 				= $dom_document->createAttribute('width');
					$dom_attribute->value 			= $request_code_size->Width;
					$dom_element_code_size->appendChild($dom_attribute);
					$dom_attribute	 				= $dom_document->createAttribute('height');
					$dom_attribute->value 			= $request_code_size->Height;
					$dom_element_code_size->appendChild($dom_attribute);
					
					if (!is_null($request_code_size->IFrame)):
						$dom_element_iframe 		= $dom_document->createElement('iframe');
						$cdata_iframe 				= $dom_document->createCDATASection($request_code_size->IFrame);
						$dom_element_iframe->appendChild($cdata_iframe);
						$dom_element_code_size->appendChild($dom_element_iframe);
					endif;
					if (!is_null($request_code_size->Javascript)):
						$dom_element_javascript 	= $dom_document->createElement('javascript');
						$cdata_javascript			= $dom_document->createCDATASection($request_code_size->Javascript);
						$dom_element_javascript->appendChild($cdata_javascript);
						$dom_element_code_size->appendChild($dom_element_javascript);
					endif;
					
					$member_name			 		= 'class_member_only_' . $member_direction . '_request_code_size_rotation';
					
					if ($request_code_size->$member_name != null):
					
						$rotation = $request_code_size->$member_name;
						
						$dom_element_rotation 				= $dom_document->createElement('rotation');	
						$dom_element_numerator 				= $dom_document->createElement('numerator', $rotation->Numerator);	
						$dom_element_denominator 			= $dom_document->createElement('denominator', $rotation->Denominator);

						$dom_element_rotation->appendChild($dom_element_numerator);
						$dom_element_rotation->appendChild($dom_element_denominator);
						
						$dom_element_code_size->appendChild($dom_element_rotation);
						
					endif;
					
					$dom_element_code->appendChild($dom_element_code_size);
					
				endforeach;
				
				$dom_element_advertisement->appendChild($dom_element_code);
				
				$member_name			 			= 'class_member_only_' . $member_direction . '_request_filter';
				
				if ($request_advertisement->$member_name != null):
					
					$dom_element_filter 			= $dom_document->createElement('filter');
					
					$request_filter = $request_advertisement->$member_name;
					
					$member_name	= 'class_member_only_' . $member_direction . '_request_filter_geo_country_list';
					
					$has_country 	= ($request_filter->$member_name != null
									 	&& count($request_filter->$member_name));

					$member_name	= 'class_member_only_' . $member_direction . '_request_filter_geo_state_list';
									 	
					$has_state 		= ($request_filter->$member_name != null
									 	&& count($request_filter->$member_name));

					$member_name	= 'class_member_only_' . $member_direction . '_request_filter_language_list';
									 	
					$has_language 	= ($request_filter->$member_name != null
									 	&& count($request_filter->$member_name));
					
										
					if ($has_country || $has_state):
						
						$dom_element_geo	 			= $dom_document->createElement('geo');
							
						if ($has_country):
						
							$dom_element_countries	 	= $dom_document->createElement('countries');
							
							$member_name				= 'class_member_only_' . $member_direction . '_request_filter_geo_country_list';
							
							foreach ($request_filter->$member_name as $filter_geo_country):
								
								$dom_element_country	= $dom_document->createElement('country', $filter_geo_country->CountryCode);
								$dom_element_countries->appendChild($dom_element_country);
								
							endforeach;

							$dom_element_geo->appendChild($dom_element_countries);
							
						endif;
						

						if ($has_state):
						
							$dom_element_states	 	= $dom_document->createElement('states');
							
							$member_name			= 'class_member_only_' . $member_direction . '_request_filter_geo_state_list';
							
							foreach ($request_filter->$member_name as $filter_geo_state):
								
								$dom_element_state	= $dom_document->createElement('state', $filter_geo_state->State);
								$dom_element_states->appendChild($dom_element_state);
								
							endforeach;

							$dom_element_geo->appendChild($dom_element_states);
							
						endif;
						
						$dom_element_filter->appendChild($dom_element_geo);
						
					endif;

					if ($has_language):
						
						$dom_element_languages	 = $dom_document->createElement('languages');
							
						$member_name			 = 'class_member_only_' . $member_direction . '_request_filter_language_list';
						
						foreach ($request_filter->$member_name as $filter_language):
								
							$dom_element_language	= $dom_document->createElement('language', $filter_language->LanguageCode);
							$dom_element_languages->appendChild($dom_element_language);
						
						endforeach;

						$dom_element_filter->appendChild($dom_element_languages);
							
					endif;

					if (!is_null($request_filter->Proxies)):
						$dom_element_proxies			= $dom_document->createElement('proxies', $this->to_boolean($request_filter->Proxies));
						$dom_element_filter->appendChild($dom_element_proxies);
					endif;
					if (!is_null($request_filter->IsMobile)):
						$dom_element_ismobile			= $dom_document->createElement('ismobile', $this->to_boolean($request_filter->IsMobile));
						$dom_element_filter->appendChild($dom_element_ismobile);
					endif;
					if (!is_null($request_filter->IsTablet)):
						$dom_element_istablet			= $dom_document->createElement('istablet', $this->to_boolean($request_filter->IsTablet));
						$dom_element_filter->appendChild($dom_element_istablet);
					endif;
					if (!is_null($request_filter->IsBrandSafe)):
						$dom_element_isbrandsafe		= $dom_document->createElement('isbrandsafe', $this->to_boolean($request_filter->IsBrandSafe));
						$dom_element_filter->appendChild($dom_element_isbrandsafe);
					endif;
					if (!is_null($request_filter->IsPorn)):
						$dom_element_isporn				= $dom_document->createElement('isporn', $this->to_boolean($request_filter->IsPorn));
						$dom_element_filter->appendChild($dom_element_isporn);
					endif;
					if (!is_null($request_filter->IsCPA)):
						$dom_element_iscpa				= $dom_document->createElement('iscpa', $this->to_boolean($request_filter->IsCPA));
						$dom_element_filter->appendChild($dom_element_iscpa);
					endif;
					if (!is_null($request_filter->IsAboveTheFold)):
						$dom_element_isabovethefold		= $dom_document->createElement('isabovethefold', $this->to_boolean($request_filter->IsAboveTheFold));
						$dom_element_filter->appendChild($dom_element_isabovethefold);
					endif;
					if (!is_null($request_filter->Category)):
						$dom_element_category			= $dom_document->createElement('category', $request_filter->Category);
						$dom_element_filter->appendChild($dom_element_category);
					endif;
					if (!is_null($request_filter->SubCategory)):
						$dom_element_subcategory			= $dom_document->createElement('subcategory', $request_filter->SubCategory);
						$dom_element_filter->appendChild($dom_element_subcategory);
					endif;
					$dom_element_advertisement->appendChild($dom_element_filter);
					
				endif;
				
				$member_name		= 'class_member_only_' . $member_direction . '_request_famous_brand_list';
				
				$has_famous_brands 	= ($request_advertisement->$member_name != null
									 	&& count($request_advertisement->$member_name));
				
				if ($has_famous_brands):
						
					$dom_element_famousbrands	 = $dom_document->createElement('famousbrands');
							
					$member_name				 = 'class_member_only_' . $member_direction . '_request_famous_brand_list';
					
					foreach ($request_advertisement->$member_name as $famous_brand):
								
						$dom_element_famous_brand	= $dom_document->createElement('brand', $famous_brand->BrandName);
						$dom_element_famousbrands->appendChild($dom_element_famous_brand);
					
					endforeach;
						
					$dom_element_advertisement->appendChild($dom_element_famousbrands);
							
				endif;

				if (!is_null($request_advertisement->AdQuality)):
					$dom_element_adquality 				= $dom_document->createElement('adquality', $request_advertisement->AdQuality);
					$dom_element_advertisement->appendChild($dom_element_adquality);
				endif;
				
				if (!is_null($request_advertisement->CPM) && $request_advertisement->CPM):
					$dom_element_cpm 					= $dom_document->createElement('cpm', $request_advertisement->CPM);
					$dom_element_advertisement->appendChild($dom_element_cpm);
				endif;
				if (!is_null($request_advertisement->CPC) && $request_advertisement->CPC):
					$dom_element_cpc 					= $dom_document->createElement('cpc', $request_advertisement->CPC);
					$dom_element_advertisement->appendChild($dom_element_cpc);
				endif;
				if (!is_null($request_advertisement->CPA) && $request_advertisement->CPA):
					$dom_element_cpa 					= $dom_document->createElement('cpa', $request_advertisement->CPA);
					$dom_element_advertisement->appendChild($dom_element_cpa);
				endif;
				$dom_element_maximpressions 		= $dom_document->createElement('maximpressions', $request_advertisement->MaxImpressions);
				$dom_element_advertisement->appendChild($dom_element_maximpressions);
				$dom_element_maxclicks		 		= $dom_document->createElement('maxclicks', $request_advertisement->MaxClicks);
				$dom_element_advertisement->appendChild($dom_element_maxclicks);
				$dom_element_maxpayout		 		= $dom_document->createElement('maxpayout', $request_advertisement->MaxPayout);
				$dom_element_advertisement->appendChild($dom_element_maxpayout);
				if (!is_null($request_advertisement->OurNumbers)):
					$dom_element_ournumbers		 		= $dom_document->createElement('ournumbers', $this->to_boolean($request_advertisement->OurNumbers));
					$dom_element_advertisement->appendChild($dom_element_ournumbers);
				endif;
				$dom_element_ournumbers		 		= $dom_document->createElement('id', $request_advertisement->ID);
				$dom_element_advertisement->appendChild($dom_element_ournumbers);
				$dom_element_name			 		= $dom_document->createElement('name', $request_advertisement->Name);
				$dom_element_advertisement->appendChild($dom_element_name);
				$dom_element_description			= $dom_document->createElement('description', $request_advertisement->Description);
				$dom_element_advertisement->appendChild($dom_element_description);
				$flightdatestart = $request_advertisement->FlightDateStart;
				$dom_element_flightdatestart		= $dom_document->createElement('flightdatestart', TimeUtils::mysql_date_to_xml_date($flightdatestart));
				$dom_element_advertisement->appendChild($dom_element_flightdatestart);
				$flightdatefinish = $request_advertisement->FlightDateFinish;
				$dom_element_flightdatefinish		= $dom_document->createElement('flightdatefinish', TimeUtils::mysql_date_to_xml_date($flightdatefinish));
				$dom_element_advertisement->appendChild($dom_element_flightdatefinish);
				$dom_element_netpayabledelaydays 	= $dom_document->createElement('netpayabledelaydays', ($request_advertisement->NetPayableDelayDays ? $request_advertisement->NetPayableDelayDays : 30));
				$dom_element_advertisement->appendChild($dom_element_netpayabledelaydays);
				if (!is_null($request_advertisement->PauseTrigger)):
					$dom_element_pausetrigger			= $dom_document->createElement('pausetrigger', $request_advertisement->PauseTrigger);
					$dom_element_advertisement->appendChild($dom_element_pausetrigger);
				endif;
				if (!is_null($request_advertisement->ResumeTrigger)):
					$dom_element_resumetrigger			= $dom_document->createElement('resumetrigger', $request_advertisement->ResumeTrigger);
					$dom_element_advertisement->appendChild($dom_element_resumetrigger);
				endif;
				$dom_element_adbundle->appendChild($dom_element_advertisement);
				
			endforeach;
			
			$member_name	= 'class_member_only_' . $member_direction . '_request_customer_contact_list';
			
			$has_contacts 	= ($request_adbundle->$member_name != null
								&& count($request_adbundle->$member_name));
			
			if ($has_contacts):

				$dom_element_customers 			= $dom_document->createElement('customers');
			
				$member_name	= 'class_member_only_' . $member_direction . '_request_customer_contact_list';
				
				foreach ($request_adbundle->$member_name as $customer_contact):
						
					$dom_element_customer 			= $dom_document->createElement('customer');
					$dom_element_customer_contact 	= $dom_document->createElement('contact');
					
					if (!is_null($customer_contact->FirstName)):
						$dom_element_firstname			= $dom_document->createElement('firstname', $customer_contact->FirstName);
						$dom_element_customer_contact->appendChild($dom_element_firstname);
					endif;
						
					if (!is_null($customer_contact->LastName)):
						$dom_element_lastname			= $dom_document->createElement('lastname', $customer_contact->LastName);
						$dom_element_customer_contact->appendChild($dom_element_lastname);
					endif;
					
					if (!is_null($customer_contact->LastName)):
						$dom_element_phone				= $dom_document->createElement('phone', $customer_contact->Phone);
						$dom_element_customer_contact->appendChild($dom_element_phone);
					endif;
					
					if (!is_null($customer_contact->Email)):
						$dom_element_email				= $dom_document->createElement('email', $customer_contact->Email);
						$dom_element_customer_contact->appendChild($dom_element_email);
					endif;

					$dom_element_customer->appendChild($dom_element_customer_contact);
					$dom_element_customers->appendChild($dom_element_customer);
					
				endforeach;
				
				$dom_element_adbundle->appendChild($dom_element_customers);
				
			endif;
			
			$member_name			= 'class_member_only_' . $member_direction . '_request_vendor_contact';
			
			if ($request_adbundle->$member_name != null):
				
					$vendor_contact 				= $request_adbundle->$member_name;
				
					$dom_element_vendor 			= $dom_document->createElement('vendor');
					$dom_element_vendor_contact 	= $dom_document->createElement('contact');
					
					$dom_element_firstname			= $dom_document->createElement('firstname', $vendor_contact->FirstName);
					$dom_element_lastname			= $dom_document->createElement('lastname', $vendor_contact->LastName);
					$dom_element_phone				= $dom_document->createElement('phone', $vendor_contact->Phone);
					$dom_element_email				= $dom_document->createElement('email', $vendor_contact->Email);
					
					$dom_element_vendor_contact->appendChild($dom_element_firstname);
					$dom_element_vendor_contact->appendChild($dom_element_lastname);
					$dom_element_vendor_contact->appendChild($dom_element_phone);
					$dom_element_vendor_contact->appendChild($dom_element_email);
					
					$dom_element_vendor->appendChild($dom_element_vendor_contact);
					
					$member_name					= 'class_member_only_' . $member_direction . '_request_vendor_partner';
					
					$vendor_partner 				= $vendor_contact->$member_name;
					$dom_element_vendor_partnerid 	= $dom_document->createElement('partnerid', $vendor_partner->PartnerID);
					$dom_element_vendor_partnername	= $dom_document->createElement('partnername', $vendor_partner->PartnerName);
					$dom_element_vendor->appendChild($dom_element_vendor_partnerid);
					$dom_element_vendor->appendChild($dom_element_vendor_partnername);
					
					$dom_element_adbundle->appendChild($dom_element_vendor);
				
			endif;
			
			$dom_element_adbundles->appendChild($dom_element_adbundle);
		
		endforeach;
		
		$dom_element_dadf_request->appendChild($dom_element_adbundles);
		$dom_document->appendChild($dom_element_dadf_request);
		$dom_document->formatOutput = true;
		
		$dadf_version = Zend_Registry::get('application.version');
		
		if ($dom_document->schemaValidate ('../application/schemas/Dadf.request-' . $dadf_version . '.xsd') == false):
			
			throw new Except("DADF schema validation failed");
			
		endif;
		
		$this->xmlData  = $dom_document->saveXML();
	

	}		
	
}